﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.HelperClasses;
using WMS.Reports;

namespace WMS
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			var loginForm = new LoginForm();
			if (loginForm.ShowDialog() != DialogResult.OK)
			{
				this.Close();
			}

			this.WindowState = FormWindowState.Maximized;
			DisableMenuItems();
		}

		private void DisableMenuItems()
		{
			//return;

			SettingsToolStripMenuItem.Enabled = (from r in UserInfo.Roles
												 where r.CanAddUser == true
												 select r).Any(); 

			ProjectsToolStripMenuItem.Enabled = (from r in UserInfo.Roles
													where r.CanAddProject == true
													select r).Any();

			PricingToolStripMenuItem.Enabled = (from r in UserInfo.Roles
													where r.CanAddPrice == true
													select r).Any();

			WarehouseToolStripMenuItem.Enabled = (from r in UserInfo.Roles
													where r.CanAddStore == true
													select r).Any();

			ProductsToolStripMenuItem.Enabled = (from r in UserInfo.Roles
													where r.CanAddItem == true
													select r).Any();

			CodingToolStripMenuItem.Enabled = (from r in UserInfo.Roles
													where r.CanAddBcode == true
													select r).Any();

			UsersToolStripMenuItem.Enabled = (from r in UserInfo.Roles
											  where r.CanAddUser == true
											  select r).Any();

			RoleToolStripMenuItem.Enabled = (from r in UserInfo.Roles
											  where r.CanAddRole == true
											  select r).Any();

			ContractorsToolStripMenuItem.Enabled = (from r in UserInfo.Roles
													where r.CanAddContractor == true
													select r).Any();

			SupplierToolStripMenuItem.Enabled = (from r in UserInfo.Roles
												 where r.CanAddSupplier == true
												 select r).Any();

			AuditToolStripMenuItem.Enabled = (from r in UserInfo.Roles
												 where r.CanAudit == true
												 select r).Any();

			ReportsToolStripMenuItem.Enabled = (from r in UserInfo.Roles
												 where r.Viewreports == true
												 select r).Any();

			DepositToolStripMenuItem.Enabled = WarehouseToolStripMenuItem.Enabled;
			WithdrawRequestToolStripMenuItem.Enabled = WarehouseToolStripMenuItem.Enabled;
			PullToolStripMenuItem.Enabled = WarehouseToolStripMenuItem.Enabled;
			MoveToolStripMenuItem.Enabled = WarehouseToolStripMenuItem.Enabled;
			JunkToolStripMenuItem.Enabled = WarehouseToolStripMenuItem.Enabled;
			initialBalanceToolStripMenuItem.Enabled = WarehouseToolStripMenuItem.Enabled;
		}

		private void ShowForm(Type formType)
		{
			// Loop on opened forms, and display the requested form if already opened
			foreach (Form openedform in this.MdiChildren)//Application.OpenForms)
			{
				if (openedform.GetType() == formType)
				{
					openedform.Focus();//openedform.Activate();
					return;
				}
				else
				{
					//close any other opened form
					openedform.Close();
				}
			}

			// else Create a new instance of the requested form.

			Form newForm = (Form)Activator.CreateInstance(formType);
			//// Set the parent form of the child window.
			newForm.MdiParent = this;
			newForm.WindowState = FormWindowState.Maximized;
			//// Display the new form.
			newForm.Show();
		}

		private void UsersToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(UserForm));
		}

		private void RoleToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(RoleForm));
		}

		private void إضافةToolStripMenuItem_Click(object sender, EventArgs e)
		{
			//ShowForm(typeof(AddItemsForm));
		}

		private void ContractorsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(ContractorForm));
		}

		private void ProjectsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(ProjectsForm));
		}

		private void SupplierToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(SuppliersForm));
		}

		private void ProductsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(ProductsForm));
		}

		private void WarehouseToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(WarehouseForm));
		}

		private void PullToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(RemoveItemsForm));
		}

		private void MoveToolStripMenuItem_Click(object sender, EventArgs e)
		{
			
		}

		private void CodingToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(CodingForm));
		}

		private void TasksToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(TasksForm));
		}

		private void JunkToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(MoveItemsToJunkForm));
		}

		private void AuditToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(AuditForm));
		}

		private void initialBalanceToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(InitialBalanceForm));
		}

		private void PricingToolStripMenuItem_Click(object sender, EventArgs e)
		{
			//ShowForm(typeof(PricingForm));
		}

		private void ChangePswdToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(ChangePswdForm));
		}

		private void خروجToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void storeBalanceReportToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(StoreBalanceForm));
		}

		private void PricingDepToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(PricingForm));
		}

		private void PricingWithdrawToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(PricingWithdrawalForm));
		}

		private void itemReportToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(ItemBalanceForm));
		}

		private void AdditionReportToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(AdditionReportForm));
		}

		private void RemoveReportToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(RemoveReportForm));
		}

		private void ItemCardRptToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(ItemCardReportForm));
		}

		private void DetailSupplyReportToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(DetailedSupplyReportForm));
		}

		private void DetailOutputToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(DetailedOutputReportForm));
		}

		private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(SettingsForm));
		}

		private void WithdrawRequestToolStripMenuItem_Click(object sender, EventArgs e)
		{
			//ShowForm(typeof(RemoveItemsRequestForm));
		}

		private void SupplyOrderToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(SupplyOrderForm));
		}

		private void ManufacturedItemsAddPermitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(AddItemsForm));
		}

		private void ItemsForSaleToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(AddItemsForSaleForm));
		}

		private void GeneralItemsAddPermitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(AddGeneralItemsForm));
		}

		private void WithdrawGeneralItemsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(RemoveGeneralItemsRequestForm));
		}

		private void WithdrawSalesItemsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(RemoveSalesItemsRequestForm));
		}

		private void WithdrawManufarcturedItemsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(RemoveManufacturedItemsRequestForm));
		}

		private void TransferRequestToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(MoveItemsForm));
		}

		private void WithdrawFromTransferToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(RemoveItemsFromTransferForm));
		}

		private void AddFromTransferToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ShowForm(typeof(AddItemsFromTransferForm));
		}

        private void tsMenuItemRetrievalPemission_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(frmRetrieveItem));
        }

        private void tspStoreBalanceWithSpecificEnd_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(StoreBalanceWithSpecificEnd));
        }

        private void BarCodeGenerationMenuItemClicked(object sender, EventArgs e)
        {
            ShowForm(typeof(BarCodeGenerator));
            //BarCodeGenerator generator = new BarCodeGenerator();
            //generator.ShowDialog();
        }

        private void DetailSupplyForAllProductsReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(DetailedSupplyReportForAllItemsForm));
        }

        private void ExaminationDocumentMenuItemClick(object sender, EventArgs e)
        {
            ShowForm(typeof(ExaminationDocument));
        }

        private void DetailOutputForAllProductsReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(DetailedOutputReportForAllItemsForm));
        }

        private void InventoryMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(InventoryReport));
        }
        private void DetailedSupplyFromToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(DetailedSupplyFormFromTo));
        }

        private void OutputFromToMenuItemClick(object sender, EventArgs e)
        {
            ShowForm(typeof(DetailedOutpotReportFromTo));
        }

        private void GardWithoutQtyMenuItem_Click(object sender, EventArgs e)
        {
            // making another constructor calling for hiding column Qty in this report calling
            var inventoryReport = new InventoryReport(true)
                                                  {
                                                      MdiParent = this,
                                                      WindowState = FormWindowState.Maximized
                                                  };
            inventoryReport.Show();
        }

        private void SupplierAnalysisMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(SupplierAnalysisReportForm));
        }

        private void IssuedAnalysisMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(IssuedAnalysisReport));
        }

        private void TransfersMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(typeof(TransferAnalysisReport));
        }

	}
}
